<?php
/**
 * Created by PhpStorm.
 * User: aoc
 * Date: 2018/6/20
 * Time: 13:51
 */

namespace app\api\controller\v1\controller\Coupon;


use app\api\controller\Api;
use app\api\controller\v1\model\Coupon\Coupon as CO;
use think\Controller;

/**
 * 代金券功能类库
 * Class Coupon
 * @package app\api\controller\v1\controller\Coupon
 */
class Coupon extends Api
//class Coupon extends Controller
{
    /**
     * 查询用户某种代金券数目
     * @param $user_id
     * @param int $coupon_id
     * @return string
     */
    public function get_coupon_count($user_id, $coupon_id=1)
    {
        if(request()->isPost())
        {
            //查询用户某种代金券数量
            $arry = CO::search_coupon($user_id, $coupon_id);
            if(!$arry['status'])
            {
                return $this->returnmsg(200,'暂无数据',false);
            }
            $coupon_num = $arry['data'];
            return $this->returnmsg(200,$arry['message'],$coupon_num);
        }
        return ['code'=>400,'message'=>'Not post'];
    }

    /**
     * 使用代金券功能
     * @param $user_id
     * @param $coupon_id
     * @return string
     */
    public function useCoupon($user_id,$coupon_id=1)
    {
        if(request()->isPost())
        {
            //查询用户代金券数量
            $arry = CO::search_coupon($user_id,$coupon_id);
            if(!$arry['status'])
            {
                return $this->returnmsg(200,$arry['message'],false);
            }
            $coupon_num = $arry['data'];
            //如果代金券数量>0，则消耗代金券并回调订单方法
            if(!($coupon_num['amout']>0) || !($coupon_num['used_amout']>0))
            {

                return $this->returnmsg(200,'代金券数量不够',false);
            }
            //更新用户代金券表，更新代金券详情表
            $is_used_cashpon = CO::consume_coupon($user_id,$arry['data']['id']);
            if($is_used_cashpon['status'])
            {
                return $this->returnmsg(200,$is_used_cashpon['message'],true);
            }
        }
        return ['code'=>400,'message'=>'has problem'];

    }

    /**
     * 赠送优惠券方法
     * @param $user_id  用户id
     * @param $coupon_id 优惠券种类 代金券为1
     * @param int $count  数量
     * @return string
     */
    public function handselCashCoupon($user_id,$coupon_id = 1,$count = 3)
    {
        if(request()->isPost())
        {
            //判断登录状态
            $status = CO::get_user_status($user_id);
            if($status == 0)
            {
                return $this->returnmsg(400,'Not login');
            }
            //操作用户对应代金券表
            $handsel_coupon = CO::handsel_coupon($user_id,$coupon_id,$count);
            if($handsel_coupon['status'])
            {
                return $this->returnmsg(200,$handsel_coupon['message'],true);
            }
        }
        return ['code'=>400,'message'=>'has problem'];
    }

    /**
     * 宠粮兑换代金券
     * @param $user_id  用户id
     * @param int $count  兑换数目
     * @return array
     */
    public function exchangeCashCoupon($user_id, $count = 1 )
    {

        if(request()->isPost())
        {
            //获取宠粮数目
            $pet_info =CO::get_pet_food($user_id);
            //宠粮数目乘以兑换代金券数目，如果宠粮不够则失败
            if($pet_info['pet_food'] < $count * 30)
            {
                return $this->returnmsg(200,'宠粮不足',false);
            }
            //如果宠粮足够，扣除宠粮增加代金券
            $exchange = CO::exchange_cash_coupon($user_id,$pet_info['pet_food_id'],$count);
            if(!$exchange['status'])
            {
                return $this->returnmsg(400,$exchange['message'],false);
            }
            return $this->returnmsg(200,$exchange['message'],true);
        }


    }


}